package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhentao.mapper.BrandMapper;
import com.zhentao.minio.service.FileStorageService;
import com.zhentao.model.dto.product.BrandDto;
import com.zhentao.model.entity.product.Brand;
import com.zhentao.model.vo.common.Result;
import com.zhentao.model.vo.common.ResultCodeEnum;
import com.zhentao.service.BrandService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author 20183
 * @description 针对表【brand(分类品牌)】的数据库操作Service实现
 * @createDate 2024-08-19 15:43:18
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand>
        implements BrandService{
    @Autowired
    private BrandMapper brandMapper;

    //    minio注入配置
    @Resource
    private FileStorageService fileStorageService;

    @SneakyThrows
    @Override
    public Result file(MultipartFile file) {
        String s = fileStorageService.uploadPicFile(file.getOriginalFilename(), file.getInputStream());
        return Result.build(s, ResultCodeEnum.SUCCESS);
    }

    @Override
    public Object brandsave(Brand brand) {
        //        判断前端传来的数据是否正确
        if(brand==null){
            return Result.build(null,ResultCodeEnum.DATA_ERROR);
        }
        Brand brand1 = new Brand();
        brand.setId(null);
        brand1.setLogo(brand.getLogo());
        brand1.setName(brand.getName());
        // 获取北京时间
        ZonedDateTime beijingTime = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
        // 将 ZonedDateTime 转换为 Date
        Date createTime = Date.from(beijingTime.toInstant());
        brand1.setCreateTime(createTime);
        brand1.setIsDeleted(0);
        brandMapper.insert(brand1);

        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Override
    public Object brandupdadte(Brand brand) {
        if(brand==null){
            return Result.build(null,ResultCodeEnum.DATA_ERROR);
        }
        Brand brand1 = new Brand();
        brand1.setId(brand.getId());
        brand1.setUpdateTime(new Date());
        brand1.setLogo(brand.getLogo());
        brand1.setName(brand.getName());
        brandMapper.updateById(brand1);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result branddlete(Long id) {
        Long deletes = brandMapper.deletes(id);
        return Result.build(deletes,ResultCodeEnum.SUCCESS);
    }

    @Override
    public PageInfo<Brand> findByPage(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Brand> brandList = brandMapper.findByPage() ;
        return new PageInfo(brandList);

    }

    @Override
    public Result brandlist(BrandDto brandDto) {
        Page<Brand> page=new Page<>(brandDto.getPageNum(),brandDto.getPageSize());
        LambdaQueryWrapper<Brand> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(Brand::getIsDeleted,0);
        Page<Brand> page1 = this.page(page,queryWrapper);
        return Result.build(page1,200,"操作成功");

    }
}




